﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Common;
using Aioms.Model;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace Aioms.Controllers
{
    /// <summary>
    /// 测试类
    /// </summary>
    [DynamicApiController]
    [ApiDescriptionSettings(Order = 5050)]
    public class TestController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public TestController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 根据数据库表生成所有Model
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetGenerateallmodel() {
            db.DbFirst.IsCreateAttribute().CreateClassFile("d:\\Demo", "Models");
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "生成所有Model成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 根据Model生成数据库表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetGenerateTable()
        {
            db.CodeFirst.InitTables(typeof(Message));
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "生成所需表成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 清空表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetTruncateTable()
        {
            db.DbMaintenance.TruncateTable("enterprise_permission");
            List<int> eids = new() { 11, 12, 13, 14, 15, 16, 19, 22, 23, 24, 25 };
            foreach (var eid in eids)
            {
                List<BasePermission> bps = await db.Queryable<BasePermission>().Where(bp => bp.IndustryType == 1).ToListAsync();
                foreach (var bp in bps)
                {
                    await db.Insertable<EnterprisePermission>(new EnterprisePermission { Eid = eid, Bspmid = bp.Id, ActionKey = bp.ActionKey, Node = bp.Node, Type = bp.Type, Path = bp.Path, Name = bp.Name, ParentId = bp.ParentId, SortId = bp.SortId, Status = 1 }).ExecuteCommandAsync();
                }
            }
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "生成企业权限表成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 创建数据库、表、测试数据/默认数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetCreateData() {
            //db.DbMaintenance.CreateDatabase();
            //db.CodeFirst.InitTables<Area>();
            //db.CodeFirst.InitTables<Department>();
            //db.CodeFirst.InitTables<Device>();
            //db.CodeFirst.InitTables<DeviceType>();
            //db.CodeFirst.InitTables<NoticeRepair>();
            //db.CodeFirst.InitTables<NrWorkOrder>();
            //db.CodeFirst.InitTables<TpWorkOrder>();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "创建数据库、表、测试数据成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        public async Task<IActionResult> PostHehe([FromForm] string str)
        {
            // select @@datadir mysqlInstallPath;
            // show variables where variable_name = 'datadir'
            TpWorkOrder workorder = await db.Queryable<TpWorkOrder>().InSingleAsync(3);
            XunJianPlan xunJianPlan = workorder.TaskInfo.ToObject<XunJianPlan>();
            var data = xunJianPlan.XunJianDians.Where(xjd => xjd.Id == 101).Select(xjd => xjd.XunJianXiangs.Where(xjx => xjx.Id == 217).Select(xjx => new { Id = xjx.Id, P = xjx.Procedure }).FirstOrDefault()).FirstOrDefault();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = data,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
